<template>
    <el-card shadow="never" class="border-0">
        <!-- 搜索 -->
        <el-form :model="searchForm" label-width="80px" class="mb-3" size="small">
            <el-row :gutter="20">
                <el-col :span="5" :offset="0">
                    <el-form-item label="真实姓名">
                        <el-input v-model="searchForm.realName" placeholder="真实姓名" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="手机号">
                        <el-input v-model="searchForm.phone" placeholder="手机号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="14">
                    <div class="flex items-center justify-end">
                        <el-button type="primary" @click="getData(1)">搜索</el-button>
                        <el-button @click="resetSearchForm">重置</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>

        <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">

            <el-table-column label="真实姓名" align="center" width="120">
                <template #default="{ row }">
                    {{ row.realName }}
                </template>
            </el-table-column>

            <el-table-column label="手机号" align="center">
                <template #default="{ row }">
                    {{ row.phone }}
                </template>
            </el-table-column>

            <el-table-column label="邮箱" align="center"  width="250">
                <template #default="{ row }">
                    {{ row.email }}
                </template>
            </el-table-column>

            <!-- <el-table-column label="性别" align="center"  width="80">
                <template #default="{ row }">
                    {{ transferSex(row.sex) }}
                </template>
            </el-table-column> -->

            <el-table-column label="状态" align="center"  width="80">
                <template #default="{ row }">
                    {{ transferWxUser_dailyState(row.dailyState) }}
                </template>
            </el-table-column>

            <el-table-column label="创建时间" align="center">
                <template #default="{ row }">
                    {{ dateFormatter(row.createTime) }}
                </template>
            </el-table-column>

            <el-table-column label="操作" width="100" align="center">
                <template #default="scope">
                    <el-button v-if="showBtnName(scope.row.id) != -1" type="success" size="small" text @click="submitAssign(scope.row)">已选择</el-button>
                    <el-button v-else type="primary" size="small" text @click="submitAssign(scope.row)">未选择</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <div class="flex items-center justify-center mt-5">
            <el-pagination background layout="prev, pager ,next" :total="total" :current-page="currentPage"
                :page-size="limit" @current-change="getData" />
        </div>
    </el-card>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue"
import { api_user_list } from "~/api/userApi.js"
import { transferSex, dateFormatter, transferWxUser_dailyState } from '~/utils/common.js'

//传递参数，已选择的工程师信息
const props = defineProps({
    assignProps: Object,
})

// table数据
const tableData = ref([])
const loading = ref(false)

// 分页
const currentPage = ref(1)
const total = ref(0)
const limit = ref(10)
// 选择的派发工程师IDS
const assignedUserId = ref([])

// 查询条件
const searchForm = reactive({
    realName: "",
    phone: "",
    page: currentPage.value,
    size: limit.value,
    //工程师用户，固定值
    roleId: 4
})

// 重置搜索框
const resetSearchForm = () => {
    searchForm.realName = ""
    searchForm.phone = ""
    getData(1)
}

// 生命周期
onMounted(() => {

    //已经选择的进行赋值
    if(null != props.assignProps && undefined != props.assignProps && props.assignProps.length>0){
        assignedUserId.value = props.assignProps
    }
    //console.log("assignedUserId.value===>",assignedUserId.value)

    //初始化列表数据
    getData()
})

// 获取数据
function getData(p = null) {
    if (typeof p == "number") {
        currentPage.value = p
    }
    loading.value = true
    searchForm.page = currentPage.value
    api_user_list(searchForm)
    .then(res => {
        //console.log(res.records)
        if (res) {
            tableData.value = res.records
            total.value = res.total
        } else {
            tableData.value = []
            total.value = 0
        }
    }).finally(() => {
        loading.value = false
    })
}

// 展示 已分配 还是 派发
// const showBtnName = (id) => {
//     let index = assignedUserId?.value?.findIndex(x => x == id)
//     return index != -1 ? '已选择' : '未选择'
// }

const showBtnName = (id) => {
    let index = assignedUserId?.value?.findIndex(x => x == id)
    return index
}

// 派单|取消派单
function submitAssign(row) {
    //console.log("select user id ===>", row.id)

    //判断已选择工程师是否已经存在
    let index = assignedUserId?.value.findIndex(x => x == row.id)
    if (index != -1) {
        //取消派发
        assignedUserId.value.splice(index, 1)
    } else {
        //派发
        assignedUserId.value.push(row.id)
    }
    //console.log("align user ids ===>", assignedUserId.value)

    //传递给调用者
    getCheckedEngineer()
}

// 对外暴露方法
const emit = defineEmits([
    'getCheckedEngineer',
])

const getCheckedEngineer = () => {
    const selectData = assignedUserId
    let returnParams = selectData.value
    emit("getCheckedEngineer", returnParams)
}

</script>
